module Shoulda
  module ActiveRecord
    module Matchers
      def be_child_to(name)
        AssociationMatcher.new(:is_child_to, name)
      end

      class AssociationMatcher
        protected
        alias_method :old_macro_description, :macro_description

        def macro_description
          @macro.to_s == 'is_child_to' ? 'be child to' : old_macro_description
        end
      end
    end

    module Macros
      def should_be_child_to(association)
        matcher = be_child_to(association)
        should matcher.description do
          mock_parent = mock
          mock_inst = subject.class.new
          mock_inst.expects(association).with.returns(mock_parent)
          
          assert_same mock_parent, mock_inst.parent
        end
      end
    end
  end
end
